package nl.ypmits.utils.as3.tween.tweener {
	import caurina.transitions.Tweener;

	import nl.ypmits.utils.as3.math.SimpleMath;

	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.filters.BlurFilter;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Rectangle;

	/**
	 * @author Dave Lenz
	 * 
	 * MotionBlurTweener is an extension on Tweener which can be used to create a directional motion blur.
	 */
	public class MotionBlurTweener {

//		private var maxSize : Number;
//		private var maxblur : Number = 40;
//		private var easeAmount : Number = .75;
//		private var offset : Number = maxSize / 2;
//		private var blur_bmp : BitmapData;
//		private var blur_rect : Rectangle;
//		private var blur_point : Point = new Point( 0, 0 );
//		private var blur_filter : BlurFilter = new BlurFilter( 0, 0 );
//		private var rotate_matrix : Matrix;
//		private var targetloc : Point;
		public var obj : Sprite;
		public var blurredClick_mc : Sprite;
		public var image : Bitmap;

		
		public function MotionBlurTweener(  ) {
			trace( "WARNING! MotionBlurTweener is a static class! Don't instantiate it please!" );
		}

		
		/**
		 * @param (second parameter)
		 * @param target Point A point with an x and an y coordinate
		 * @param maxblur Number A maximum bluramount
		 */
		public static function addTween( obj : Object, args : Object ) : Boolean {
			if ( !Boolean( obj ) ) return false;
			
			var objects : Array = [];
			if( obj is Array ) {
				for (var i : int = 0; i < obj.length ; i++) {
					objects = obj.concat( );
				}
			} else {
				objects = [ obj ];
			}
			
			startTween( objects, args );
			
			return true;
		}

		
		public static function startTween( objects : Array, args : Object ) : void {
			
			var item : Sprite;
			var parent : Sprite;
			var angle : Number;
			var distance : Number;
			var maxsize : Number;
			
			for (var i : int = 0; i < objects.length ; i++) {
				item = objects[i];
				parent = Sprite( item.parent );

				
				maxsize = args.maxblur + Math.sqrt( item.width * item.width + item.height * item.height );
				angle = SimpleMath.getAngle( new Point( item.x, item.y ), args.target );
				distance = Point.distance( new Point( item.x, item.y ), args.target );
				
				item.rotation = angle;
				
				var itemrect : Rectangle = item.getBounds( parent );
				
				
				
				var m : Matrix = new Matrix( );
				m.rotate( SimpleMath.degreesToRadians( angle ) );
				
				var image : Bitmap = new Bitmap( );
				image.bitmapData = new BitmapData( item.width + 100, item.height, false, 0xFF9999 );
				image.bitmapData.draw( item, m, null, null, itemrect, false );
				image.bitmapData.applyFilter( image.bitmapData, itemrect, new Point( item.x, item.y ), new BlurFilter( 40, 0, 3 ) );
				image.x = item.x;
				image.y = item.y;
				item.addChild( image );
				
				// GROEN KADER
//				parent.graphics.lineStyle( 1, 0x009900, 1 );
//				parent.graphics.drawRect( item.x, item.y, itemrect.width, itemrect.height );
//				
//				parent.graphics.lineStyle( 1, 0xFF9900, 1 );
//				parent.graphics.moveTo( item.x, item.y );
//				parent.graphics.lineTo( args.target.x, args.target.y );
				
				trace( "{maxsize:", maxsize + ", angle:", angle + ", distance:", distance + "}" );
				
				Tweener.addTween( item, {x:args.target.x, y:args.target.y, time:.5, transition:"easeInOutSine", onUpdate:updateFunc, onUpdateParams:[ item ]} );
			}
		}

		
		public static function updateFunc( item : Sprite ) : void {
		//	trace("MotionBlurTweener.updateFunc("+[item]+")");
		}
	}
}
